﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace MP_toMPG
{
    class MP_Converter
    {
        static void Main(string[] args)
        {
            //args = new string[1];
            //args[0] = @"E:\a2-kocho.mp_";
            Console.WriteLine("简易工具，生成完成后将自动退出\n\n注意1：后缀名需为小写mp_,大写可能会出错！\n注意2：原先生成的mpg将被覆盖！");
            for (int i = 0; i < args.Length; i++)
            {
                ConvertMPG(args[i]);
                Console.WriteLine(args[i]+"转换完成！");
            }
            
        }


        private static void ConvertMPG(String Input)
        {
            FileStream MP_ = File.Open(Input, FileMode.Open);
            int ll=Input.Length;
            String Output = Input;
            Output=Output.Replace("mp_", "mpg");
            long length = MP_.Length;
            //偏移计数器
            int round = 0;
            long count=0;
            //MPG开始结尾标志
            int[] SearchHex = new int[] { 0x00, 0x00, 0x01, 0xB9, 0x00, 0x00, 0x01, 0xBA };
                   //MPGStart = new int[] { 0x00, 0x00, 0x01, 0xBA },
                   //  MPGEND = new int[] { 0x00, 0x00, 0x01, 0xBA };
            int tp;
            while(round<2)
            {
                if (count == length)
                {
                    round++;
                    SearchHex = new int[] { 0x00, 0x00, 0x01, 0xB7, 0x00, 0x00, 0x01, 0xB3 };
                    count = 0;
                    MP_.Seek(0, 0);
                }

                tp = MP_.ReadByte();
                count++;
                //if(tp==0xFF)
                //{
                //    tp = MP_.ReadByte();
                //    count++;
                    if (tp == 0x00)
                    {
                        int[] btp = new int[8] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

                        for (int i = 1; i < 8; i++)
                        {
                            btp[i] = MP_.ReadByte();
                            count++;
                            
                        }
                        if (CompareBt(btp, SearchHex))
                        {
                            count = count - 4;
                            break;
                        }
                        else
                        {
                            //此处为修正读取7位有个可能引起的Bug
                            count = count - 7;
                            MP_.Seek(-7, SeekOrigin.Current);
                        }
                    }

                //}
                    
            }
            if (count != length)
            {
                byte[] bpart = new byte[count];
                byte[] apart = new byte[length - count];
                MP_.Seek(0, 0);
                MP_.Read(bpart, 0, bpart.Length);
                MP_.Read(apart, 0, apart.Length);
                MP_.Flush();
                if(File.Exists(Output))
                {
                    File.Delete(Output);
                }
                FileStream MPG = File.Open(Output, FileMode.CreateNew);
                MPG.Write(apart, 0, apart.Length);
                MPG.Write(bpart, 0, bpart.Length);
                MPG.Flush();
                MPG.Close();
            }
            else
            {
                Console.WriteLine("不能被转换");
            }
        }

        /// <summary>
        /// 比较两个数组是否相等
        /// </summary>
        /// <param name="b1">数组1</param>
        /// <param name="b2">数组2</param>
        /// <returns>是否相等</returns>
        private static bool CompareBt(int[] b1, int[] b2)
        {
            if (b1.Length != b2.Length)
            { 
                return false; 
            }
            if (b1 == null || b2 == null)
            { 
                return false;
            }
            for (int i = 0; i < b1.Length; i++)
            {
                if (b1[i] != b2[i])
                    return false;
            }
            return true;
        }

    }


}
